﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace CodeText
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //Text = folderBrowserDialog1.SelectedPath;
                string [] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, textBox1.Text, SearchOption.AllDirectories);
                listView1.Items.Clear();
                foreach (string file in files)
                {
                    ListViewItem lvi = new ListViewItem(file);
                    listView1.Items.Add(lvi);
                }
            }
        }



        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {                  
                  contextMenuStrip1.Show(Cursor.Position);
            }
        }

        private void 移除选中ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            while (this.listView1.SelectedItems.Count != 0)
            {
                this.listView1.Items.Remove(this.listView1.SelectedItems[0]);
            }  
        }

        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Move);
        }

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            //判断是否选择拖放的项，

            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }

            //定义项的坐标点
            Point cp = listView1.PointToClient(new Point(e.X, e.Y));//
            ListViewItem dragToItem = listView1.GetItemAt(cp.X, cp.Y);

            if (dragToItem == null)
            {
                return;
            }

            int dragIndex = dragToItem.Index;
            ListViewItem[] sel = new ListViewItem[listView1.SelectedItems.Count];

            for (int i = 0; i <= listView1.SelectedItems.Count - 1; i++)
            {
                sel[i] = listView1.SelectedItems[i];
            }

            for (int i = 0; i < sel.GetLength(0); i++)
            {
                ListViewItem dragItem = sel[i];

                int itemIndex = dragIndex;

                if (itemIndex == dragItem.Index)
                {
                    return;
                }

                if (dragItem.Index < itemIndex)
                    itemIndex++;
                else
                    itemIndex = dragIndex + i;

                ListViewItem insertItem = (ListViewItem)dragItem.Clone();

                listView1.Items.Insert(itemIndex, insertItem);

                listView1.Items.Remove(dragItem);
            }
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            for (int i = 0; i <= e.Data.GetFormats().Length - 1; i++)
            {
               // if (e.Data.GetFormats().Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
                {
                    e.Effect = DragDropEffects.Move;
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            //遍历每个文件
            foreach (ListViewItem item in listView1.Items)
            {
                //sb.AppendLine("//===================================================================");
                sb.AppendLine("File:" + item.Text);
                try
                {
                    String [] lines = File.ReadAllLines(item.Text);
                    foreach (string line in lines)
                    {
                        if (!String.IsNullOrEmpty(line.Trim()))
                        {
                            sb.AppendLine(line);
                        }
                    }

                    
                }
                catch { }

                //sb.AppendLine("//===================================================================");
            }
            //1）删除//开始的注释 
            String result =  Regex.Replace(sb.ToString(), @"//.*", "");
            //2）删除“/*  */”之间的注释
            result = Regex.Replace(result, @"/\*.*?\*/", "");

            sb.Replace("\r\n", "\n");
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllText(sfd.FileName, sb.ToString());
            }
        }
    }
}
